package com.liucxu.jiandanl.View.Fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.liucxu.jiandanl.Base.BaseFragment;
import com.liucxu.jiandanl.Business.BaseEvent;
import com.liucxu.jiandanl.Business.JokesEvent;
import com.liucxu.jiandanl.Entity.PicComments;
import com.liucxu.jiandanl.R;
import com.liucxu.jiandanl.View.Adapter.JokesAdapter;
import com.liucxu.jiandanl.Widget.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * 段子对应的Fragment
 */
public class JokeFragment extends BaseFragment {
    @InjectView(R.id.refresh_layout)
    RefreshLayout refresh_layout;
    @InjectView(R.id.listview)
    ListView listview;
    private boolean isRefreshing = true;
    private int page_index = 1;
    private View view;
    JokesAdapter adapter;
    private List<PicComments> posts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        isRegister = true;
        super.onCreate(savedInstanceState);
        posts = new ArrayList<PicComments>();
        adapter = new JokesAdapter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_fresh_news, container, false);
            refresh_layout = (RefreshLayout) view.findViewById(R.id.refresh_layout);
            listview = (ListView) view.findViewById(R.id.listview);
            listview.setAdapter(adapter);
            refresh_layout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue);
            refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    isRefreshing = true;
                    JokesEvent.GetJokesByPage(1);
                }
            });
            refresh_layout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    isRefreshing = false;
                    JokesEvent.GetJokesByPage(page_index+1);
                }
            });
            refresh_layout.setRefreshing(true);
            JokesEvent.GetJokesByPage(1);
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
            return view;
        }
        return view;
    }

    public void onEventMainThread(JokesEvent event) {
        refresh_layout.setRefreshing(false);
        if (event.getResultCode() == BaseEvent.HTTP_STATUS_OK) {
            if (event.getStatus().equals("ok")) {
                if (isRefreshing)
                {
                    page_index = 1;
                    posts.clear();
                    posts.addAll(event.getComments());
                }
                else{
                    page_index = page_index + 1;
                    posts.addAll(event.getComments());
                }
                adapter.setDatas(posts);
            } else {
                Toast.makeText(getActivity(), "网络错误请重试", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getActivity(), "网络错误请重试", Toast.LENGTH_SHORT).show();
        }
    }
}
